package com.kongge.main.config;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by kongge on 2017/11/16.
 * description:
 * layoutid:
 */
public class FunctionManager {

    public static final int DEFAULT_CONFIG = 0;
    public static final int ALL_CONFING = 10000;
    private final static String FILENAME = "function.properties";
    private static Properties properties;

    // ~ 属性值start ===============================================================================
    public static final String IS_LOG_OPEN                                    = "is_log_open"; // 日志是否打开，0关闭，10000开启；默认0
    // ~ 属性值end   ===============================================================================

    public FunctionManager() {
    }

    public static void initFuction(Context context) {
        initConfig(context);
    }

    private static boolean initConfig(Context context) {
        if (context == null) {
            return false;
        }
        InputStream in = null;
        try {
            in = context.getAssets().open(FILENAME);
            properties = new Properties();
            properties.load(in);
        } catch (Exception e) {
            properties = null;
            e.printStackTrace();
            return false;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static int getProperty(String key, int defaultValue) {
        if (properties == null) {
            return defaultValue;
        }
        String value = properties.getProperty(key);
        if (value != null) {
            try {
                return Integer.parseInt(value);
            } catch (Exception e) {

            }
        }
        return defaultValue;
    }

    public static String getProperty(String key, String defaultValue) {
        if (properties == null) {
            return defaultValue;
        }
        String value = properties.getProperty(key);
        if (value != null) {
            return value;
        }
        return defaultValue;
    }

    public static boolean getPropertyOpen(String key, boolean defaultOpen) {
        if (properties == null) {
            return defaultOpen;
        }
        int value = getProperty(key, DEFAULT_CONFIG);
        if (value == ALL_CONFING) {
            return true;
        }

        return defaultOpen;
    }

    public static boolean getPropertyIsAllConfig(String key) {
        int value = getProperty(key, DEFAULT_CONFIG);
        if (value == ALL_CONFING) {
            return true;
        }
        return false;
    }

}
